package com.aidex.biz.controller;

import com.aidex.biz.domain.Taizhang002;
import com.aidex.biz.service.Taizhang002Service;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.AjaxResult;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 重要子公司基本情况Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-05-22
 */
@RestController
@RequestMapping("/biz/bizTaizhang002")
public class Taizhang002Controller extends BaseController {
    @Autowired
    private Taizhang002Service taizhang002Service;

    /**
     * 查询重要子公司基本情况列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizTaizhang002:list')")
    @GetMapping("/list")
    public R<PageInfo> list(Taizhang002 taizhang002, HttpServletRequest request, HttpServletResponse response) {
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            taizhang002.setCompanyId(null);
        } else {
            taizhang002.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        taizhang002.setPage(new PageDomain(request, response));
        return R.data(taizhang002Service.findPage(taizhang002));
    }

    /**
     * 获取重要子公司基本情况详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizTaizhang002:query')")
    @GetMapping(value = "/{id}")
    public R<Taizhang002> detail(@PathVariable("id") String id) {
        return R.data(taizhang002Service.get(id));
    }

    /**
     * 新增重要子公司基本情况
     */
    @PreAuthorize("@ss.hasPermi('biz:bizTaizhang002:add')")
    @Log(title = "重要子公司基本情况", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated Taizhang002 taizhang002) {
        return R.status(taizhang002Service.save(taizhang002));
    }

    /**
     * 修改重要子公司基本情况
     */
    @PreAuthorize("@ss.hasPermi('biz:bizTaizhang002:edit')")
    @Log(title = "重要子公司基本情况", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated Taizhang002 taizhang002) {
        return R.status(taizhang002Service.save(taizhang002));
    }


    /**
     * 删除重要子公司基本情况
     */
    @PreAuthorize("@ss.hasPermi('biz:bizTaizhang002:remove')")
    @Log(title = "重要子公司基本情况", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(taizhang002Service.deleteTaizhang002ByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizTaizhang002:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(taizhang002Service.findMaxSort(new Taizhang002()));
    }

    /**
     * 导出重要子公司基本情况列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizTaizhang002:export')")
    @Log(title = "重要子公司基本情况", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(Taizhang002 taizhang002) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            taizhang002.setCompanyId(null);
        } else {
            taizhang002.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<Taizhang002> list = taizhang002Service.findList(taizhang002);
        ExcelUtil<Taizhang002> util = new ExcelUtil<Taizhang002>(Taizhang002.class);
        return util.exportExcel(list, "重要子公司基本情况数据");
    }

    @Log(title = "重要子公司基本情况导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<Taizhang002> util = new ExcelUtil<Taizhang002>(Taizhang002.class);
        List<Taizhang002> taizhang002List = util.importExcel(file.getInputStream());
        String message = taizhang002Service.importData(taizhang002List);
        return AjaxResult.success(message);
    }


}
